package com.senlan.water.core;

import cn.dev33.satoken.exception.NotLoginException;
import com.senlan.water.common.exception.BusinessException;
import com.senlan.water.common.result.BaseResult;
import com.senlan.water.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public BaseResult<ResultCode> handlerException(Exception e) {
        log.error("系统异常：{}",e.getMessage(),e);
        return BaseResult.error(ResultCode.ERROR);
    }

    @ExceptionHandler(NotLoginException.class)
    public BaseResult<ResultCode> handlerNotLoginException(Exception e) {
        return BaseResult.error(ResultCode.USER_AUTH_EXPIRED);
    }

    @ExceptionHandler(BusinessException.class)
    public BaseResult<ResultCode> handlerProjectException(BusinessException e) {
        log.warn("业务异常：{}",e.getMessage(),e);
        return BaseResult.error(e.getCode(),e.getMessage());
    }

}
